package com.ktjiaoyu.utils;

import java.util.ArrayList;
import java.util.List;

public class Page<T> {
    //当前页码
    private Integer pageNo = 1;
    //每页显示数量
    private Integer pageSize = 5;
    //总记录数
    private Integer totalCount = null;
    //总页数
    private Integer totalPageCount = 1;
    //当前页数据
    private List<T> data;

    public Page() {
    }

    public Page(Integer pageNo, Integer pageSize, Integer totalCount, Integer totalPageCount, List<T> data) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.totalPageCount = totalPageCount;
        this.data = data;
    }

    /**
     * 获取
     * @return pageNo
     */
    public Integer getPageNo() {
        return pageNo;
    }

    /**
     * 设置
     * @param pageNo
     */
    public void setPageNo(Integer pageNo) {
        if (pageNo <= 0){
            pageNo = 1;
        }
        if (totalCount != null){
            //计算过总页数后，页码的设置要参考总页数
            if (totalPageCount >= 1 && pageNo > totalPageCount){
                pageNo = totalPageCount;
            }
            this.pageNo = pageNo;
        }
    }

    /**
     * 获取
     * @return pageSize
     */
    public Integer getPageSize() {
        return pageSize;
    }

    /**
     * 设置
     * @param pageSize
     */
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获取
     * @return totalCount
     */
    public Integer getTotalCount() {
        return totalCount;
    }

    /**
     * 设置
     * @param totalCount
     */
    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
        this.totalPageCount = this.totalCount % this.pageSize == 0
                ? this.totalCount / this.pageSize
                : this.totalCount / this.pageSize + 1;
    }

    /**
     * 获取
     * @return totalPageCount
     */
    public Integer getTotalPageCount() {
        return totalPageCount;
    }

    /**
     * 设置
     * @param totalPageCount
     */
    public void setTotalPageCount(Integer totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    /**
     * 获取
     * @return data
     */
    public List<T> getData() {
        return data;
    }

    /**
     * 设置
     * @param data
     */
    public void setData(List<T> data) {
        this.data = data;
    }

    public int offset(){
        return (pageNo - 1) * this.pageSize;
    }
    //是否第一页
    public boolean isFirst(){
        return pageNo == 1;
    }
    //是否最后一页
    public boolean isLast(){
        return pageNo == totalPageCount;
    }
    //是否还有下一页
    public boolean hasNext(){
        return pageNo < totalPageCount;
    }
    //是否还有上一页
    public boolean hasPrevious(){
        return pageNo > 1;
    }
    //页面上显示指定长度的页码
    public List<Integer> getShowPageNos(int showCount){
        List<Integer> list = new ArrayList<Integer>();
        //先加中间页码
        list.add(pageNo);
        //每次页面计算的步长
        int step = 1;
        while (list.size() < showCount){
            boolean added = false;
            int left = pageNo - step;   //左边减页码
            int right = pageNo + step;  //右边加页码
            if (left > 1){
                list.add(0, left);
                added = true;
            }
            if (right <= totalPageCount){
                list.add(right);
                added = true;
            }
            if (!added){
             //如果左侧都没加成功，则退出
                break;
            }
            step++;
        }
        return list;
    }
}
